﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.linqDAL;
using dhDataAcess.sysDAL;


namespace dhSystemBLL.orderData
{
   public  class productListBLL
    {


       productUnitModel productUnitRow = new productUnitModel();
        private productListTableAdapter _productListAdapter = null;
        private productListTableAdapter productListAdapter
        {
            get
            {
                if (_productListAdapter == null) _productListAdapter = new productListTableAdapter();
                return _productListAdapter;
            }
        }
        /// <summary>
        /// 查询产品信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.productListDataTable getproductListAll()
        {
            try
            {
                return productListAdapter.GetDataproductListAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据产品名称查询信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.productListDataTable getproductListAll(string pname)
        {
            try
            {
                return productListAdapter.GetDataproductListBypname (pname);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据产品名称查询信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.productListDataTable getproductListAllByptype(string ptype)
        {
            try
            {
                return productListAdapter.GetDataproductListByptype (ptype);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
             /// <summary>
        /// 插入信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productListInsert(productListModel row, orderDataSet.productUnitDataTable pTable, string state)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productListDAL productList = new productListDAL(tn, conn);
                productUnitDAL productUnit = new productUnitDAL(tn, conn);
                basListDAL basList = new basListDAL(tn, conn);
                if (state == "新增")
                {
                    productList.productListInsert(row);
                }
                if (state == "编辑")
                {
                    productList.productListUpdate(row);
                }
                foreach (orderDataSet.productUnitRow detailRow in pTable)
                {
                    if (detailRow.RowState != DataRowState.Deleted)
                    {
                     
                        productUnitRow.createDate = DateTime.Now;
                        productUnitRow.createUser = "";
                        productUnitRow.updateDate = DateTime.Now;
                        productUnitRow.updateUser = "";
                        if (!detailRow.IsmarkNull())
                        {
                            productUnitRow.mark = detailRow.mark;
                        }
                        else
                        {
                            productUnitRow.mark = "";
                        }
                    }

                    //编辑子表记录,新增
                    if (detailRow.RowState == DataRowState.Added)
                    {

                        productUnit.productUnitInsert(productUnitRow);
                        //更新单位编号

                        string orderSerial = basicSql.getOrderSerail("单位编号");
                        basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "单位编号");
                    }
                    //修改
                    if (detailRow.RowState == DataRowState.Modified)
                    {
                        productUnit.productUnitUpdate(productUnitRow);
                    }
                    //删除
                    if (detailRow.RowState == DataRowState.Deleted)
                    {
                        DataRow delrow = (DataRow)detailRow;
                        productUnit.productUnitDel(delrow["unitNo", DataRowVersion.Original].ToString());
                    }


                }
                tn.Commit();
                return i;


            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个产品名称已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 插入信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productListInsert(productListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productListDAL productList = new productListDAL(tn, conn);
                productList.productListInsert(row);
                tn.Commit();
                return i;
                

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个产品名称已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 导入原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productListimoportInsert(orderDataSet.productListDataTable productTable)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productListDAL product = new productListDAL(tn, conn);
                product.productimportInsert  (productTable);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个产品名称已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productListUpdate(productListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productListDAL productList = new productListDAL(tn, conn);
                productList.productListUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除一行信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productListDel(string pname)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productListDAL productList = new productListDAL(tn, conn);
                productList.productListDel (pname );
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
               
            }
            finally
            {
                conn.Close();
            }
        }
        //public SqlDataReader  productListretquery(List<queryModel> queryList)
        //{
        //    int i = 0;
        //    SqlConnection conn = dataConn.connOpen();
        //    SqlTransaction tn = null;
        //    try
        //    {
        //        tn = conn.BeginTransaction();
        //        productListDAL productList = new productListDAL(tn, conn);
        //      SqlDataReader query=  productList.productListretquery (queryList );
        //        tn.Commit();
        //        return query;
        //    }
        //    catch (Exception ex)
        //    {
        //        tn.Rollback();
        //        throw new Exception(ex.Message.ToString());

        //    }
        //    finally
        //    {
        //        conn.Close();
        //    }
        //}
    }
}
